task('build', function (params) {

    console.log('This is the default task.');
  
    console.log('Generating Parser...');
    
    var fs = require('fs'), Parser = require('jison').Parser, grammar = require('./grammar.js').grammar;
    fs.writeFileSync('gen_parser.js', (new Parser(grammar)).generate());
    
    console.log('Successfully generated Parser.');
    
    var code = '', _code = '', i,  mods = ['nodes', 'lexer', 'gen_parser', 'sql_parser'], len = mods.length, mod = "";
    
    for(i = 0; i < len; i++){
    
        mod = mods[i];
        
        _code += [
            "require['./" + mod + "'] = new function(){"
           ,"   var exports = this; "
           ,"   " + fs.readFileSync('./' + mod + '.js')
           ,"};"
        ].join('\n') + '\n';
        
    }
    
    console.log('Combining SQL parser..');
    
    code = [
        "(function(root) { "
        ,"  var SQLParser = function() { "
        ,"      function require(path){ return require[path]; } "
        ,("          " + _code)
        ,"      return require['./sql_parser']; "
        ,"  }(); "
        ,"  if(typeof define === 'function' && define.amd) { "
        ,"      define(function() { return SQLParser }); "
        ,"  }"
        ,"  else{"
        ,"      root.SQLParser = SQLParser"
        ,"   } "
        ,"}(this)); "
    ].join('\n');

    fs.writeFileSync('./sql_parser_all.js', code);

    console.log('Successfully built SQL parser');
  
    console.log('Creating SQL parser for re-js..');
  
    code =  fs.readFileSync('sql_parser_re_js_template_start.js',  'utf-8');
    
    code = code.replace("_license_", fs.readFileSync("LICENSE.txt"));
  
    code = code + _code + fs.readFileSync('sql_parser_re_js_template_end.js',  'utf-8');
    
    fs.writeFileSync('../standard/re.sqlparser2.js', code);

});

task('test', function(){

    var sqlParser = require('./sql_parser');
    
    console.log(sqlParser.parse("SELECT * FROM tbl"));

});